package com.example.amoxicilin.null_test.main;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.util.ArrayList;
import java.util.List;

public class InfoActivity extends AppCompatActivity {

    private Button confirm;
    private EditText account;
    private EditText introduction;
    private EditText sex;
    private EditText college;
    private EditText college2;
    private CheckBox art;
    private CheckBox fl;
    private CheckBox cs;
    private CheckBox law;
    private CheckBox ce;
    private CheckBox sport;
    private CheckBox ecnmy;
    private CheckBox elctrn;
    private int number;
    String str1,str2,str3,str4,str5= "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        Bundle bundle=getIntent().getExtras();
        number=bundle.getInt("number");

        initComponents();
        final List<CheckBox> checkBoxList = new ArrayList<CheckBox>();

        checkBoxList.add(art);
        checkBoxList.add(fl);
        checkBoxList.add(cs);
        checkBoxList.add(law);
        checkBoxList.add(ce);
        checkBoxList.add(sport);
        checkBoxList.add(ecnmy);
        checkBoxList.add(elctrn);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int label1, label2, label3;
                int count = 0;
                String isChecked = "";

                str1 = account.getText().toString();
                str2 = introduction.getText().toString();
                str3 = sex.getText().toString();
                str4 = college.getText().toString();
                str5 = college2.getText().toString();

                for (int i=0; i<checkBoxList.size() ; i++) {
                    if(checkBoxList.get(i).isChecked()) {
                        isChecked = isChecked + String.valueOf(i+1);
                        count++;
                        if(count > 3) {
                            final AlertDialog alertDialog = new AlertDialog.Builder(InfoActivity.this).create();
                            alertDialog.setCancelable(false);
                            alertDialog.setTitle("错误");
                            alertDialog.setMessage("选择的标签数不能超过3个");
                            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            alertDialog.cancel();
                                        }
                                    }
                            );
                            alertDialog.show();
                            isChecked = "";
                            break;
                        }

                    }
                }
                label1 = (int) isChecked.charAt(0)-48;
                label2 = (int) isChecked.charAt(1)-48;
                label3 = (int) isChecked.charAt(2)-48;

                String path = "http://120.24.94.172/JsonTestServer/jsonTest?" +
                        "type1=modify&type2=user&name="+str1+"&introduction="+str2+"&label1="+label1+"&label2="+label2+"&label3="+label3+
                        "&sex="+str3+"&college="+str4+"&college2="+str5+"&number="+number;
                String jsonString = HttpUtils.getJsonContent(path);
                Log.i("Test",path);
                Status status= JsonTools.getStatus(null,jsonString);

                if (status.getStatusId()==1)
                {
                    new android.app.AlertDialog.Builder(InfoActivity.this)
                            .setTitle("系统消息")
                            .setMessage("修改成功！").setPositiveButton("确认",new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    }).show();
                }
                else{
                    new android.app.AlertDialog.Builder(InfoActivity.this)
                            .setTitle("系统消息")
                            .setMessage("修改失败！").setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }).show();
                }

            }
        });
    }
    public void initComponents() {
        confirm = (Button) findViewById(R.id.confirm);
        account = (EditText) findViewById(R.id.et_account);
        introduction = (EditText) findViewById(R.id.et_introduction);
        sex = (EditText) findViewById(R.id.et_sex);
        college = (EditText) findViewById(R.id.et_college);
        college2 = (EditText) findViewById(R.id.et_college2);
        art = (CheckBox) findViewById(R.id.cb_art);
        fl = (CheckBox) findViewById(R.id.cb_fl);
        cs = (CheckBox) findViewById(R.id.cb_cs);
        law = (CheckBox) findViewById(R.id.cb_law);
        ce = (CheckBox) findViewById(R.id.cb_ce);
        sport = (CheckBox) findViewById(R.id.cb_sport);
        ecnmy = (CheckBox) findViewById(R.id.cb_ecnmy);
        elctrn = (CheckBox) findViewById(R.id.cb_elctrn);
    }

}
